package com.tao.controller;

import com.tao.mapper.CommentMapper;
import com.tao.pojo.dto.CommentDTO;
import com.tao.pojo.entity.Comment;
import com.tao.pojo.vo.CommentVO;
import com.tao.pojo.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @RequestMapping("/comment/insert")
    public int insert(@RequestBody CommentDTO commentDTO, HttpSession session){
        System.out.println(commentDTO);
        UserVO user = (UserVO) session.getAttribute("user");
        if (user==null){
            return 2;
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setUserId(user.getId());
        comment.setWeiboId(commentDTO.getWeiboId());
        comment.setCreated(new Date());
        System.out.println(comment);
        commentMapper.insert(comment);
        return 1;
    }
    @RequestMapping("/comment/select")
    public List<CommentVO> select(){
        return commentMapper.selectByWeiboId();
    }

}
